import keras
from keras.layers.normalization import BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten,Lambda
from keras.layers.normalization import BatchNormalization
from keras.layers import Convolution2D, MaxPooling2D
from keras.preprocessing.image import load_img,img_to_array
from keras.optimizers import SGD, Adam, RMSprop
from keras.callbacks import EarlyStopping,History
from keras.preprocessing.image import load_img
import cv2
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline
data = pd.read_csv("driving_log.csv")
data.head()
img_left=load_img('IMG/left_2016_12_01_13_30_48_287.jpg')
img_center=load_img('IMG/center_2016_12_01_13_30_48_287.jpg')
img_right=load_img('IMG/right_2016_12_01_13_30_48_287.jpg')
f,(ax1,ax2,ax3)=plt.subplots(1,3,figsize=(24,8))
ax1.imshow(img_left)
ax1.set_xticks([]);ax1.set_yticks([])
ax1.set_title("Left image",fontsize=20)
ax2.imshow(img_center)
ax2.set_xticks([]);ax2.set_yticks([])
ax2.set_title("Center image",fontsize=20)
ax3.imshow(img_right)
ax3.set_xticks([]);ax3.set_yticks([])
ax3.set_title("Right image",fontsize=20)
f.tight_layout()
img=load_img('IMG/center_2016_12_01_13_30_48_287.jpg')
img = np.array(img)
cropped_img = img[40:img.shape[0]-25,:]
resized = cv2.resize(cropped_img,(200,66), interpolation=cv2.INTER_AREA)
f,(ax1,ax2,ax3) = plt.subplots(1,3,figsize=(24,8))
ax1.imshow(img)
ax1.set_xticks([]);ax1.set_yticks([])
ax1.set_title("Original image",fontsize=20)
ax2.imshow(cropped_img)
ax2.set_xticks([]);ax2.set_yticks([])
ax2.set_title("Cropped image",fontsize=20)
ax3.imshow(resized)
ax3.set_xticks([]);ax3.set_yticks([])
ax3.set_title("Resized image",fontsize=20)
f.tight_layout()
img=load_img('IMG/center_2016_12_01_13_30_48_287.jpg')
img_yuv = cv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2YUV)
f,(ax1,ax2) = plt.subplots(1,2,figsize=(24,8))
ax1.imshow(img)
ax1.set_xticks([]);ax1.set_yticks([])
ax1.set_title("RGB color space",fontsize=20)
ax2.imshow(img_yuv)
ax2.set_xticks([]);ax2.set_yticks([])
ax2.set_title("YUV color space",fontsize=20)
def augment_brightness(img):
img = cv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2HSV)
random_bright = 0.25+np.random.uniform()
img[:,:,2] = img[:,:,2]*random_bright
img = cv2.cvtColor(img,cv2.COLOR_HSV2RGB)
return img,random_bright
img=load_img('IMG/center_2016_12_01_13_30_48_287.jpg')
fig = plt.figure(figsize=(16,10))
for i in range(10):
bright_img, bright = augment_brightness(img)
subplot = fig.add_subplot(10,10,(i+1))
subplot.set_xticks([])
subplot.set_yticks([])
subplot.imshow(bright_img,interpolation="nearest")
subplot.set_title(str(bright*100)[:4]+"%")
def transpose_image(img,steering):
img = cv2.flip(img,1)
return img,-1.0*steering
original = data.loc[1430]
steering = original["steering"]
original_img = load_img(original["center"])
original_img = np.array(original_img)
flipped,flipped_steering = transpose_image(original_img,steering)
f,(ax1,ax2)=plt.subplots(1,2,figsize=(24,8))
ax1.imshow(original_img)
ax1.set_xticks([]);ax1.set_yticks([])
ax1.set_title("Steering "+str(steering)[:4],fontsize=20)
ax2.imshow(flipped)
ax2.set_xticks([]);ax2.set_yticks([])
ax2.set_title("Steering "+str(flipped_steering)[:5],fontsize=20)
driving_log = pd.read_csv("driving_log.csv").reset_index()
plt.hist(driving_log["steering"],bins=100)
plt.xlabel("Steering",fontsize=15)
plt.ylabel("Number of data",fontsize=15)
driving_log = pd.read_csv("driving_log.csv").reset_index()
num_drops = int(len(driving_log[np.abs(driving_log["steering"])<=0.1])*0.75)
drop_lows = driving_log[driving_log["steering"]==0]["index"].values[0:num_drops]
driving_log_new = driving_log.drop(drop_lows,axis=0).sample(frac=1.0)
num_training=(int(len(driving_log_new)*0.8))
training_data = driving_log_new[0:num_training]
validation_data = driving_log_new[num_training:]
plt.hist(training_data["steering"],bins=100,label="Training set")
plt.hist(validation_data["steering"],bins=100,label="Validation set")
plt.legend(loc="best")
plt.xlabel("Steering",fontsize=15)
plt.ylabel("Number of data",fontsize=15)